/* 
 * Copyright (c) 2010	All Right Reserved
 * http://www.cndatacom.com
 * Project  kpiPlatform 
 */
package com.fare.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 相关说明：主要用来说明此类的用途
 * 
 * @since Oct 29, 2010
 * @author tss
 * @version $Id: StringUtils.java,v1.0 Oct 29, 2010 10:57:12 AM tss Exp $
 *
 */
public class StringUtils {
	
	/**
	 * 判断一个字符串是否为数字类型
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean isNumber(String str) {
		Pattern p = Pattern.compile("\\p{Digit}+(\\.\\p{Digit}+)?");
		Matcher m = p.matcher(str);
		if (m.matches()) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 将配置文件中的表达式 ${aa} 替换为 contex 中的aa的所对应的键值
	 * @param context
	 * @param str
	 * @return
	 */
	public static String replaceByContextMap(Map<String, String> context, String str) {
		if(isEmpty(str)){
			return "";
		}
		Pattern p = Pattern.compile("\\$\\{(.*?)\\}");
		Matcher m = p.matcher(str);
		StringBuffer sb= new StringBuffer();
		while(m.find()) {			
			String temp = context.get(m.group(1));
			m.appendReplacement(sb, temp+"");			
			//System.out.println(sb.toString());
		}
		m.appendTail(sb);
		return sb.toString();
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isTrue(String str){	
		if("true".equalsIgnoreCase(str)||"Y".equalsIgnoreCase(str)||"YES".equalsIgnoreCase(str)){
			return true;
		}
		return false;
	}
	
	
	 /**
     * <p>Checks if a String is empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     *
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the String.
     * That functionality is available in isBlank().</p>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is empty or null
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
   
   
	
}
